/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.web.form.QuotationSelectSpdcfForm;
import com.wii.elpro.dao.QuotationDAOInterface;
import com.wii.elpro.dao.hb.QuotationDAOImpl;
import com.wii.elpro.dao.hb.SpdcfDAOImpl;
import com.wii.elpro.entity.Quotation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class QuotationGenerateSpdcfAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionForward af = null;
        QuotationSelectSpdcfForm f = (QuotationSelectSpdcfForm) form;
        QuotationDAOInterface dao = new QuotationDAOImpl();
        List<Quotation> quotations = null;
        HttpSession sess = request.getSession(true);
        ActionMessages messages;
        try {
            quotations = dao.getQuotations(new SpdcfDAOImpl().getSpdcf(f.getSpdcf()).getInquery().getNoQuotation());
            if(quotations.size()!=0) {
                request.setAttribute("revisionNumber", quotations.get(quotations.size()-1).getRevise());
            }
            
            af = new ActionForward(mapping.findForward("success").getPath()+"?step=generate&id="+f.getSpdcf());
        } catch (Exception ex) {
            messages = new ActionMessages();
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve data. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            af = new ActionForward(mapping.findForward("fail"));
        }
        return af;
    }
}
